package com.giw.project.file_manager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.HashMap;

public class SplitFile {
	private final static File fInput = new File(
			"/home/giorgio/Scrivania/GIW/progetto/0-grafo/part-r-00000");

	private final static String tmp_path1 = "/home/giorgio/Scrivania/GIW/progetto/0-grafo/Split/litteral.txt";
	private final static String tmp_path2 = "/home/giorgio/Scrivania/GIW/progetto/0-grafo/Split/movie.txt";
	private final static String tmp_path3 = "/home/giorgio/Scrivania/GIW/progetto/0-grafo/Split/director.txt";
	private final static String tmp_path4 = "/home/giorgio/Scrivania/GIW/progetto/0-grafo/Split/genre.txt";
	private final static String tmp_path5 = "/home/giorgio/Scrivania/GIW/progetto/0-grafo/Split/actor.txt";
	private final static String tmp_path6 = "/home/giorgio/Scrivania/GIW/progetto/0-grafo/Split/else.txt";

	private static PrintStream OutputLitteral = null;
	private static PrintStream OutputMovie = null;
	private static PrintStream OutputDirector = null;
	private static PrintStream OutputGenre = null;
	private static PrintStream OutputActor = null;
	private static PrintStream OutputElse = null;

	HashMap<String, PrintStream> uri2file = new HashMap<String, PrintStream>();

	

	public void run() {
		try {
			FileOutputStream fMovie = new FileOutputStream(tmp_path2);
			FileOutputStream fDirector = new FileOutputStream(tmp_path3);
			FileOutputStream fGenre = new FileOutputStream(tmp_path4);
			FileOutputStream fActor = new FileOutputStream(tmp_path5);
			FileOutputStream fElse = new FileOutputStream(tmp_path6);
			FileOutputStream fLitteral = new FileOutputStream(tmp_path1);

			OutputLitteral = new PrintStream(fLitteral);
			OutputMovie = new PrintStream(fMovie);
			OutputDirector = new PrintStream(fDirector);
			OutputGenre = new PrintStream(fGenre);
			OutputActor = new PrintStream(fActor);
			OutputElse = new PrintStream(fElse);
			
			uri2file.put("string", OutputLitteral);
			uri2file.put("<http://data.linkedmdb.org/resource/film/", OutputMovie);
			uri2file.put("<http://data.linkedmdb.org/resource/director/",
					OutputDirector);
			uri2file.put("<http://data.linkedmdb.org/resource/film_genre/",
					OutputGenre);
			uri2file.put("<http://data.linkedmdb.org/resource/actor/", OutputActor);
			uri2file.put("else", OutputElse);

			FileInputStream fisInput = new FileInputStream(fInput);

			InputStreamReader isrInput = new InputStreamReader(fisInput);

			BufferedReader brInput = new BufferedReader(isrInput);

			String lineaInput = brInput.readLine();

			while (lineaInput != null) {

				String currentNode = lineaInput.split("\t")[0];
				
				this.getNodeKind(currentNode).println(lineaInput);
				
				lineaInput = brInput.readLine();

			}
			brInput.close();
			isrInput.close();
			OutputLitteral.close();
			OutputMovie.close();
			OutputDirector.close();
			OutputGenre.close();
			OutputActor.close();
			OutputElse.close();
			fisInput.close();
			fLitteral.close();
			fMovie.close();
			fDirector.close();
			fGenre.close();
			fActor.close();
			fElse.close();

		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}

	public static void main(String[] args) throws FileNotFoundException {
		SplitFile s = new SplitFile();
		s.run();
	}

	private PrintStream getNodeKind(String uri) {
		if (!uri.contains("http")) {
			return uri2file.get("string");
		}
		String[] parts_uri = uri.split("/");
		int i;
		uri = "";
		for (i = 0; i < parts_uri.length - 1; i++) {
			uri += "/" + parts_uri[i];
		}
		if (uri2file.get(uri.substring(1) + "/") != null)
			return uri2file.get(uri.substring(1) + "/");
		else
			return uri2file.get("else");

	}

}
